package top.infopub.mgr.bpa.web;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.dto.bpa.BpaPublicProInfoDto;
import top.infopub.dto.bpa.SettlePlanDetailDto;
import top.infopub.dto.bpa.SettlePlanListDto;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.mgr.bpa.dao.BpaSettlePlanDao;
import top.infopub.mgr.bpa.jsonVo.SettlePlanDetailJsonVo;
import top.infopub.mgr.bpa.service.BpaProcessManageService;
import top.infopub.mgr.bpa.service.BpaSettlePlanService;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.hr.service.StaffInfoService;
import top.infopub.mgr.mkt.service.ProjectInfoService;
import top.infopub.model.bpa.SettlePlan;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;

/**
 * 结算策划页面
 */

@Controller
@RequestMapping("/settlePlan")
public class BpaSettlePlanController extends GenericFlowController {

    @Autowired
    private BpaSettlePlanService bpaSettlePlanService;

    @Autowired
    private BpaSettlePlanDao bpaSettlePlanDao;

    @Autowired
    private BpaProcessManageService bpaProcessManageService;

    @Autowired
    private StaffInfoService staffInfoService;

    @Autowired
    private ProjectInfoService projectInfoService;

    /**
     * 跳转列表页
     *
     * @return
     */
    @RequestMapping("/list")
    public String list(HttpServletRequest request) {
        return "bpa/lay-settlePlan-list";
    }

    /**
     * 商务策划列表数据
     *
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<SettlePlanListDto> queryDataByCondition(SettlePlan settlePlan, ParamPage paramPage) {
        Page<SettlePlanListDto> page = new Page<SettlePlanListDto>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());

        page = bpaSettlePlanService.selectByDomainWhere(page, settlePlan);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    @RequestMapping("/toDealOrDetail")
    public String toDealOrDetail(HttpServletRequest request, String id, String proCode) {
        request.setAttribute("id", id);
        SettlePlan settlePlan = bpaSettlePlanDao.queryInfoById(id);
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        request.setAttribute("isAuditFlag", "1");
        request.setAttribute("auditTabFlag", "0");//登录人和审批人一致使用该字段是否显示审
        if (settlePlan != null) {
            if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), settlePlan.getProcessStatus())
                    || StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS9.getCode(), settlePlan.getProcessStatus())) {
                request.setAttribute("isAuditFlag", "0");
            }
            if (StringUtils.isNotBlank(settlePlan.getProcessId()) && settlePlan.getProcessId().length() > 10) {
                request.setAttribute("processId", settlePlan.getProcessId());
                String auditTabFlag = bpaProcessManageService.checkShowAuditTab(settlePlan.getProcessId(), vo);
                if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), settlePlan.getProcessStatus())
                        && StringUtils.equals("0", auditTabFlag)) {
                    request.setAttribute("isAuditFlag", "0");
                }
                request.setAttribute("auditTabFlag", auditTabFlag);
            }
        }
        request.setAttribute("proCode", proCode);
        return "bpa/lay-SettlementPlanning-detail";
    }

    @RequestMapping("/queryDetail")
    @ResponseBody
    public RspVo queryDetail(String id) {
        SettlePlanDetailJsonVo detailJson = bpaSettlePlanService.getDetail(id);
        return RspVo.getSuccessResponse(detailJson);
    }

    /**
     * 结算策划-新增  0=保存，1=提交
     * @throws ParseException
     */
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(String bodyStr, Integer submitType) {

        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();

        String str = StringEscapeUtils.unescapeJava(bodyStr);
        SettlePlanDetailJsonVo detailJson = JSON.parseObject(str, SettlePlanDetailJsonVo.class);
        RspVo rspVo = bpaSettlePlanService.save(detailJson,submitType,vo);
        return rspVo;
    }

    //根据procode获取项目的基本信息
    @RequestMapping("/queryProjDetail")
    @ResponseBody
    public RspVo queryProjDetail(String proCode) {
        SettlePlanDetailJsonVo settPlanJson = new SettlePlanDetailJsonVo();
        //项目的基础信息
        BpaPublicProInfoDto dto = projectInfoService.queryBpaProInfoByProCode(proCode);
        SettlePlanDetailDto info = new SettlePlanDetailDto();
        BeanUtils.copyProperties(dto, info);
        settPlanJson.setInfo(info);
        return RspVo.getSuccessResponse(settPlanJson);
    }


}
